package com.bdqn.service;

import com.bdqn.common.PageSupport;
import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* SysUserService服务层实现类
*/
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource // 使用注解的方式将DAO层的Bean注入
    private SysUserMapper sysUserMapper;

    /**
     * 根据用户登录账号获取用户信息
     *
     * @param account 账号
     * @param password 密码
     * @return
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null) {
            return null;
        }
        return sysUserMapper.getByAccountAndPassword(account, password);
    }

    /**
     * 保存用户
     *
     * @param sysUser
     * @return
     */
    // @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean add(SysUser sysUser) {
        // 模拟在一个事务中两次对数据库的操作，第一次操作完成之后发生异常时，第二次未操作未执行，第一次操作也将回滚。两次操作要么都不成功，要么同时成功

        sysUser.setCreatedTime(new Date()); // 设置创建时间
        int result = sysUserMapper.add(sysUser);

        // int num = 5 / 0; // 模拟程序中抛出异常
        // if (sysUser != null) {
        // 手动抛出异常
        // throw new SendEMailException("发送通知邮件异常");
        // }
        // sysUserMapper.add(sysUser);
        return (result == 1);
    }

    /**
     * 修改用户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean update(SysUser sysUser) {
        sysUser.setUpdatedTime(new Date()); // 设置修改时间
        return sysUserMapper.update(sysUser);
    }

    /**
     * 保存用户
     *
     * @param userId
     * @return
     */
    @Override
    public boolean delById(Long userId) {
        return sysUserMapper.delete(userId) == 1;
    }

    /**
     * 通过id获得单个用户对象
     *
     * @param id
     * @return
     */
    @Override
    public SysUser getById(Long id) {
        return sysUserMapper.getById(id);
    }

    /**
     * 检查账号是否存在
     *
     * @param account
     * @return
     */
    @Override
    public SysUser getAccountExist(String account) {
        return sysUserMapper.getByAccount(account);
    }

    // @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        return sysUserMapper.getList(sysUser);
    }

    /**
     * 根据条件获得总记录数
     *
     * @param sysUser
     * @return
     */
    @Override
    public int getPageListTotal(SysUser sysUser) {
        return sysUserMapper.getPageListTotal(sysUser);
    }

    /**
     * 获得用户列表
     *
     * @param sysUser
     * @return
     */
    @Override
    public List<SysUser> getPageList(SysUser sysUser, PageSupport page) {
        int pageIndex = (page.getCurrentPageNo() - 1) * page.getPageSize();
        return sysUserMapper.getPageList(sysUser, pageIndex, page.getPageSize());
    }


    @Override
    public int setUpdPassword(Long id, String newPassword, SysUser pwdSession) {
        //查询验证
        SysUser byId = sysUserMapper.getById(id);
        if (pwdSession == null || !byId.equals(pwdSession.getPassword())){    //Session不存在或者密码已被修改 修改失败
            return 0;
        }
        //修改密码
        return sysUserMapper.setUpdPassword(id,newPassword);
    }
}
